package com.huilian.iotbox.admin.wap.controller;

import com.alibaba.druid.util.StringUtils;
import com.huilian.iotbox.admin.wap.aopLog.OperateLog;
import com.huilian.iotbox.admin.wap.dao.MemberDao;
import com.huilian.iotbox.admin.wap.service.BlackListService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.BlackListDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 龙 路 飞
 * @CreateDate: 2021-11-25 16:26
 * @Description:
 */
@RestController
@RequestMapping("/api/blackList")
public class BlackListController extends UserInfoController {
    @Autowired
    private BlackListService blackListService;

    @OperateLog
    @PostMapping("/insert")
    public ServerResponse insert(@RequestBody BlackListDto blackListDto, HttpServletRequest request) {
        blackListDto.setWhy(StringUtils.isEmpty(blackListDto.getWhy()) ? null : blackListDto.getWhy());
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            blackListDto.setUserId(userInfo.getParentId());
        } else {
            blackListDto.setUserId(userInfo.getId());
        }
        return blackListService.insert(blackListDto);
    }


    @OperateLog
    @DeleteMapping("/delete")
    public ServerResponse delete(@RequestBody BlackListDto blackListDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            blackListDto.setUserId(userInfo.getParentId());
        } else {
            blackListDto.setUserId(userInfo.getId());
        }
        return blackListService.delete(blackListDto);
    }

    @PostMapping("/findListPages")
    public ServerResponse findListPages(@RequestBody BlackListDto blackListDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        blackListDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(blackListService.findListPages(blackListDto));
    }

}
